package com.vcredit.creditcat.service.speedloan;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.json.JSON;
import com.vcredit.creditcat.dto.creditrepository.ItemStatusDto;
import com.vcredit.creditcat.dto.speedloan.CheckCustResultDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.model.CustSupplementItem;
import com.vcredit.creditcat.model.ProductCheckcustItem;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.annotation.Log;


public class CommonProductCheckCustService implements IProductCheckCustService{

	private Logger logger = LoggerFactory.getLogger(CommonProductCheckCustService.class);
	
	public static final String REJECT_MSG = "交单未通过";

	@Autowired
	private CustProductApplyService custProductApplyService;

	@Log
	public CheckCustResultDto checkCust(Cust cust, Long productId,
			List<ProductCheckcustItem> checkcustItems,
			List<ItemStatusDto> itemStatusDtos,
			List<CustSupplementItem> supplementItemList,
			CustProductApply newApplyProduct) throws ParseException, IOException{
		
		if(Utility.isNullOrEmpty(checkcustItems)) {
			return CheckCustResultDto.success();
		}
		
		if(newApplyProduct != null 
				&& REJECT_MSG.equals(newApplyProduct.getRejectMsg())
				&& newApplyProduct.getIsApply()
				&& ConcurrentDateUtil.strToDate(newApplyProduct.getCreditApplyTime()).after(new Date())) {
			return CheckCustResultDto.fail(REJECT_MSG);
		}
		
		List<Long> doneAuthItems = this.getDoneAuthItems(itemStatusDtos);
		List<Long> doneSupplementItems = this.getDoneSupplementItems(supplementItemList);
		
		if(doneAuthItems.size() == 0 && doneSupplementItems.size() == 0) {
			return CheckCustResultDto.success();
		}
		
		for(ProductCheckcustItem checkItem : checkcustItems) {
			CheckCustResultDto checkCustResultDto = null;
			if(checkItem.getItemType() == 0) { // 针对原始认证项
				if(doneAuthItems.contains(checkItem.getItemId())) {
					logger.info("可交单规则请求开始=====产品ID:"+productId+",custId:" + cust.getCustId());
					checkCustResultDto = this.handlerCheckItem(checkItem, 0, cust, productId);
					logger.info("可交单规则请求结束=====返回结果:"+ JSON.json(checkCustResultDto).toString());
				}
			} else { // 针对补充认证项
				if(doneSupplementItems.contains(checkItem.getItemId())) {
					checkCustResultDto = this.handlerCheckItem(checkItem, 1, cust, productId);
				}
			}
			if(null != checkCustResultDto && checkCustResultDto.getCheckResult() == 2) {
				this.handlerCheckUserFail(cust.getCustId(),productId,newApplyProduct);
				return checkCustResultDto;
			}
		}
		return CheckCustResultDto.success();
	}
	
	protected CheckCustResultDto handlerCheckItem(ProductCheckcustItem checkItem, int itemType, 
			Cust cust, Long productId) {
		return CheckCustResultDto.success();
	}
	
	private void handlerCheckUserFail(Long custId, Long productId, CustProductApply newApplyProduct) {
		newApplyProduct = custProductApplyService.findNewApplyProduct(productId, custId);
		if(null == newApplyProduct) {
			CustProductApply custProductApply = new CustProductApply();
			custProductApply.setCustId(custId);
			custProductApply.setProductId(productId);
			custProductApply.setAuditResult(0);
			custProductApply.setRejectMsg(REJECT_MSG);
			custProductApply.setIsApply(true);
			custProductApply.setCreditApplyTime(ConcurrentDateUtil.formatDateTime(DateUtils.addDays(new Date(), 30)));
			custProductApplyService.insert(custProductApply);
		} 
		
		else {
			newApplyProduct.setAuditResult(0);
			newApplyProduct.setRejectMsg(REJECT_MSG);
			newApplyProduct.setIsApply(true);
			newApplyProduct.setCreditApplyTime(ConcurrentDateUtil.formatDateTime(DateUtils.addDays(new Date(), 30)));
			custProductApplyService.update(newApplyProduct);
		}
	}
	
	private List<Long> getDoneAuthItems(List<ItemStatusDto> itemStatusDtos) {
		List<Long> doneAuthItems = new ArrayList<Long>();
		for(ItemStatusDto itemStatus : itemStatusDtos) {
			if("1".equals(itemStatus.getStatus())) {
				doneAuthItems.add(itemStatus.getItemId());
			}
		}
		return doneAuthItems;
	}
	
	private List<Long> getDoneSupplementItems(List<CustSupplementItem> supplementItemList) {
		List<Long> doneSupplementItems = new ArrayList<Long>();
		for(CustSupplementItem supplementItem : supplementItemList) {
			if(supplementItem.getStatus() == 1) {
				doneSupplementItems.add(supplementItem.getSupplementItemId());
			}
		}
		return doneSupplementItems;
	}
}
